package org.example.day12;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Timer;
import java.util.TimerTask;

/**
 * 5.1.1.3 多个TimerTask任务及延时的测试.
 *      debug看任务队列中任务的数量和位置。
 *      ★注意: 多个任务是串行执行的，在mainLoop方法中
 */
public class MyThread9 {
    private static Timer timer = new Timer();
    static public class MyTask1 extends TimerTask {
        @Override
        public void run() {
            try {
                System.out.println("MyTask1 运行了! 时间为: " + new Date());
                Thread.sleep(10000);
                System.out.println("MyTask1 结束了~ 时间为: " + new Date());
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }
    }
    static public class MyTask2 extends TimerTask {
        @Override
        public void run() {
            System.out.println("MyTask2 运行了! 时间为: " + new Date());
            System.out.println("MyTask2 结束了~ 时间为: " + new Date());
        }
    }

    public static void main(String[] args) {
        try {
            MyTask1 task1 = new MyTask1();
            MyTask2 task2 = new MyTask2();
            SimpleDateFormat sdf1 = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
            SimpleDateFormat sdf2 = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
            String dateString1 = "2022-05-16 11:09:00";
            String dateString2 = "2022-05-16 11:10:00";
            Date dateRef1 = sdf1.parse(dateString1);
            Date dateRef2 = sdf2.parse(dateString2);
            System.out.println("字符串1时间: " + dateRef1.toLocaleString() + " 当前时间: " + new Date().toLocaleString());
            System.out.println("字符串2时间: " + dateRef2.toLocaleString() + " 当前时间: " + new Date().toLocaleString());
            timer.schedule(task1,dateRef1);
            timer.schedule(task2,dateRef2);
        } catch (ParseException e) {
            e.printStackTrace();
        }
    }
}
